/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.sysAdmin;

import controllers.changePassword.RandomPassword;
import controllers.utils.DatabaseHjelper;
import controllers.utils.SendMail;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.User;

/**
 *
 * @author dybvik
 */
@SessionScoped
@Named(value="newUser")
public class AddNewUser implements Serializable{
    private User newUser;
    private DatabaseHjelper dbh = DatabaseHjelper.hent();

    /** Creates a new instance of executeNewUser */
    public AddNewUser() {
        this.newUser = new User();
    }

    public User getNewUser() {
        return newUser;
    }

    public String addNewUser(){
        newUser.setFirstName(null);
        newUser.setLastName(null);
        newUser.setEmail(null);
        newUser.setUserName(null);
        newUser.setRoles(null);
        return "addNewUser";
    }

    public String executeNewUser(){
        addNewUserToDB();
        return "addNewUserOutput";
    }

    private void addNewUserToDB() {
        setRandomPassword();
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("INSERT INTO users(userName, userFirstName, userLastName, password, email) VALUES(?, ?, ?, ?, ?)");
            System.out.println(newUser.getUserName());
            stmt.setString(1, newUser.getUserName());
            stmt.setString(2, newUser.getFirstName());
            stmt.setString(3, newUser.getLastName());
            stmt.setString(4, newUser.getPassword());
            stmt.setString(5, newUser.getEmail());
            stmt.executeUpdate();
            stmt = conn.prepareStatement("INSERT INTO userRole(userName, role) values(?, ?)");
            for(String s: newUser.getRoles()){
                stmt.setString(1, newUser.getUserName());
                stmt.setString(2, s);
                stmt.executeUpdate();
            }
            sendMail(newUser);
        } catch (SQLException e) {
            System.out.println("Feil i lagring av bruker: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    private void sendMail(User u){
        String content = "Hei " + u.getFirstName() + " " + u.getLastName()
                +". Velkommen som bruker hos Emner'R'Us. Nedenfor finner du din påloggingsinformasjon."
                + "\n\nBrukernavn: " + u.getUserName() + "\nPassord: " + u.getPassword()
                + "\n\n Mvh GRPS-10\nSystemeringsprosjekt 2011";
        String subject = "Bruker opprettet hos Emne'R'Us.";
        SendMail sM = new SendMail(u.getEmail(), content, subject);
        sM.run();
    }

    private void setRandomPassword() {
        newUser.setPassword(RandomPassword.getRandomString(6));
    }
    
     // <editor-fold defaultstate="collapsed" desc="Getters and setters passing / fetching data to / from newUser-object. Click on the + sign to see the methods.">
/*
    public String getEmail() {
        return newUser.getEmail();
    }

    public void setEmail(String email) {
        newUser.setEmail(email);
    }

    public String getFirstName() {
        return newUser.getFirstName();
    }

    public void setFirstName(String firstName) {
        newUser.setFirstName(firstName);
    }

    public String getLastName() {
        return newUser.getLastName();
    }

    public void setLastName(String lastName) {
        newUser.setLastName(lastName);
    }

    public String getPassword() {
        return newUser.getPassword();
    }

    public void setPassword(String password) {
        newUser.setPassword(password);
    }

    public List<String> getRoles() {
        return newUser.getRoles();
    }

    public void setRoles(List<String> roles) {
        newUser.setRoles(roles);
    }

    public void setRole(String role){
        newUser.setRole(role);
    }

    public int getUserID() {
        return newUser.getUserID();
    }

    public void setUserID(int userID) {
        newUser.setUserID(userID);
    }

    public String getUserName() {
        return newUser.getUserName();
    }

    public void setUserName(String userName) {
        newUser.setUserName(userName);
    }

    public boolean isEditable() {
        return newUser.isEditable();
    }

    public void setEditable(boolean editable) {
        newUser.setEditable(editable);
    }

 
*/
     // </editor-fold>

}
